package lincen.javame.light.ui.geometric;

/***
 * ��ѧ���߰�
 * @author None
 */
public abstract class MathUtil {
	private static final double sq2p1 = 2.414213562373095048802e0;
	private static final double sq2m1 = .414213562373095048802e0;
	private static final double p4 = .161536412982230228262e2;
	private static final double p3 = .26842548195503973794141e3;
	private static final double p2 = .11530293515404850115428136e4;
	private static final double p1 = .178040631643319697105464587e4;
	private static final double p0 = .89678597403663861959987488e3;
	private static final double q4 = .5895697050844462222791e2;
	private static final double q3 = .536265374031215315104235e3;
	private static final double q2 = .16667838148816337184521798e4;
	private static final double q1 = .207933497444540981287275926e4;
	private static final double q0 = .89678597403663861962481162e3;
	private static final double PIO2 = 1.5707963267948966135E0;
	
	/**
	 * ������dת��Ϊ��ͨ�Ķ���
	 * @param d ����
	 * @return ��
	 */
	private static final double curvatureRadianToNormal(double d){
		return d*180/Math.PI;
	}

	/**
	 * �����tan ���ؽǶ�ֵ
	 * @param result
	 * @return
	 */
	public static final double arctan(double result) {
		if (result > 0) {
			return curvatureRadianToNormal(msatan(result));
		}
		return curvatureRadianToNormal(-msatan(-result));
	}
	/**
	 * ����**����ֵ
	 * @param result
	 * @return
	 */
	private static double msatan(final double result) {
		if (result < sq2m1) {
			return mxatan(result);
		}
		if (result > sq2p1) {
			return PIO2 - mxatan(1 / result);
		}
		return PIO2 / 2 + mxatan((result - 1) / (result + 1));
	}
	
	private static double mxatan(final double result) {
		double argsq, value;
	
		argsq = result * result;
		value = ((((p4 * argsq + p3) * argsq + p2) * argsq + p1) * argsq + p0);
		value = value / (((((argsq + q4) * argsq + q3) * argsq + q2) * argsq + q1) * argsq + q0);
		return value * result;
	}
	/**
	 * ���� sqrt(x2 +y2)�����м���������硣
	 */
	public static float hypot(float x, float y) {
		return (float) Math.sqrt(x * x + y * y);
	}
	
	/**
	 * ����dת��Ϊ����
	 * @param d ��
	 * @return ����
	 */
	private static final double curvatureNormalToRadian(double d){
		return d/180*Math.PI;
	}
	
	/**
	 * ����d������
	 * @param d �Ƕ�
	 * @return
	 */
	public static final double cos(double d){
		return Math.cos(curvatureNormalToRadian(d));
	}

	/**
	 * ����d������
	 * @param d �Ƕ�
	 * @return
	 */
	public static final double sin(double d){
		return Math.sin(curvatureNormalToRadian(d));
	}
}